package com.gxa.weixing.portal.service.impl;

import com.gxa.weixing.core.common.ExceptionCode;
import com.gxa.weixing.core.exception.BusinessException;
import com.gxa.weixing.core.pojo.*;
import com.gxa.weixing.core.service.impl.BaseServiceImpl;
import com.gxa.weixing.core.util.JsonUtil;
import com.gxa.weixing.core.util.SnowFlake;
import com.gxa.weixing.core.util.VerifyParamUtil;
import com.gxa.weixing.portal.mapper.GoodsDetailMapper;
import com.gxa.weixing.portal.mapper.GoodsMapper;
import com.gxa.weixing.portal.mapper.OrderGoodsMapper;
import com.gxa.weixing.portal.mapper.OrderMapper;
import com.gxa.weixing.portal.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * @Author HHT
 * 2022/1/6
 */
@Service
@Transactional
public class OrderServiceImpl extends BaseServiceImpl<Order> implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    OrderGoodsMapper orderGoodsMapper;

    @Autowired
    GoodsDetailMapper goodsDetailMapper;
    /**
     * 获取用户订单
     *
     * @param id     用户编号
     * @param status 订单状态
     * @return
     */
    @Override
    public List<Map<String, Object>> getOrder(Long id, Integer status) {
        List<Map<String, Object>> order = orderMapper.getOrder(id, status);
        return order;
    }

    /**
     * 获取订单详情
     *
     * @param id 订单编号
     * @return
     */
    @Override
    public List<Map<String, Object>> getOrderDetail(Long id) {
        List<Map<String, Object>> orderDetail = orderMapper.getOrderDetail(id);
        return orderDetail;
    }

    @Override
    public List<GetGoodsFromCartVO> settlement(List list,Double price,Long userId) {
        HashOperations<String, Object, Object> ops = redisTemplate.opsForHash();
        Map<Object, Object> entries = ops.entries(GoodsServiceImpl.CART+userId);
        List<GetGoodsFromCartVO> list1=new ArrayList<>();
        Set<Object> objects = entries.keySet();
        for (Object object : objects) {
            Goods goods = JsonUtil.stringToObject(object.toString(), Goods.class);
            for (Object aLong : list) {
                Long a=Long.parseLong(aLong.toString());
                if (goods.getId().equals(a)){
                  GetGoodsFromCartVO getGoodsFromCartVO=new GetGoodsFromCartVO();
                    getGoodsFromCartVO.setId(goods.getId());
                    getGoodsFromCartVO.setDesc(goods.getDesc());
                    getGoodsFromCartVO.setImg(goods.getImg());
                    getGoodsFromCartVO.setName(goods.getName());
                    getGoodsFromCartVO.setPrePrice(goods.getPrePrice());
                    getGoodsFromCartVO.setPrice(goods.getPrice());
                    getGoodsFromCartVO.setPurchasePrice(goods.getPurchasePrice());
                    getGoodsFromCartVO.setState(goods.getState());
                    Object o = ops.get(GoodsServiceImpl.CART + userId, object);
                    getGoodsFromCartVO.setNum(Integer.valueOf(o.toString()));
                    list1.add(getGoodsFromCartVO);
                }
            }
        }

        return list1;
    }

    @Override
    public Order SubmitOrder(Order order) throws BusinessException {

        Long orderNo = SnowFlake.nextId();

        List<OrderGoods> list = order.getOrderGoods();
        for (OrderGoods orderGoods : list) {
            orderGoods.setOrderNo(orderNo.toString());
            //先判断库存够不够
            Long goodsNumber = orderGoods.getGoodsNumber();
            GoodsDetail goodsDetail=new GoodsDetail();
            goodsDetail.setGoodsId(orderGoods.getGoodsId());
            List<GoodsDetail> select = goodsDetailMapper.select(goodsDetail);
            if (select.get(0).getSurplusInventory()< goodsNumber)
            {
                throw new BusinessException(ExceptionCode.KUCUN_NOUENOUGN);
            }
            Long surplusInventory=select.get(0).getSurplusInventory()-goodsNumber;
            select.get(0).setSurplusInventory(Integer.valueOf(surplusInventory.toString()));
            goodsDetailMapper.updateByPrimaryKey(select.get(0));
            orderGoodsMapper.insert(orderGoods);
        }
        order.setOrderNo(orderNo.toString());
        order.setStatus(0);
        order.setOrderTime(new Date(System.currentTimeMillis()));
        order.setAddressId(1L);
        order.setRuleId(1L);
        String payNo = UUID.randomUUID().toString().replace("-", "");
        order.setPayNo(payNo);
        order.setPayStatus(false);
        order.setPayFreight(new BigDecimal(3));
        orderMapper.insert(order);
        return order;

    }


}
